﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VictorBlaga.DocumentManagement.Core.Models.Repositories;
using VictorBlaga.DocumentManagement.Infrastructure;
using VictorBlaga.DocumentManagement.Models;

namespace VictorBlaga.DocumentManagement.Helpers.Validators.Impl
{
    public class UserValidator : IUserValidator
    {
        private List<ValidationError> _insertValidationErrors;

        private UserViewModel _viewModel;
        private readonly IUserRepository _userRepository;

        public UserValidator(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public UserViewModel ViewModel
        {
            get { return _viewModel; }
            set
            {
                _insertValidationErrors = new List<ValidationError>();
                _viewModel = value;
            }
        }

        public bool IsInsertValid()
        {
            var errors = new List<ValidationError>();
            if (ViewModel != null)
            {
                //Username required
                if (String.IsNullOrEmpty(_viewModel.Username))
                    errors.Add(new ValidationError("Username required.", "Username"));
                //Username unique
                else if (!_userRepository.IsUniqueUserName(0, _viewModel.Username))
                    errors.Add(new ValidationError("Username already exists", "Username"));
                //Username contains illegal characters
                else if (!ValidationHelper.IsValidUsername(_viewModel.Username))
                    errors.Add(new ValidationError("Username contains illegal characters. Use only letters, number , '_' or '.'", "Username"));

                //Password requires
                if (String.IsNullOrEmpty(_viewModel.Password))
                    errors.Add(new ValidationError("Password required", "Password"));
                //Password must match
                else if (_viewModel.Password.Length < 6)
                    errors.Add(new ValidationError("Password must be at least 6 characters long", "Password"));
                //Password must be confirmed
                else if (_viewModel.Password != _viewModel.PasswordConfirm)
                    errors.Add(new ValidationError("Password must be confirmed", "PasswordConfirm"));

                //Email required
                if (String.IsNullOrEmpty(_viewModel.Email))
                    errors.Add(new ValidationError("Email required", "Email"));
                //Email must be unique
                else if (!_userRepository.IsUniqueEmail(0, _viewModel.Email))
                    errors.Add(new ValidationError("Email already exists", "Email"));
                //Email must be valid
                else if (!ValidationHelper.IsValidEmail(_viewModel.Email))
                    errors.Add(new ValidationError("Email is not valid", "Email"));


                //For UserLevel = 0, WorkshopID must not be NULL
                if (_viewModel.UserLevel == 0 && _viewModel.WorkshopID == null)
                    errors.Add(new ValidationError("User must belong to a Workshop", "WorkshopID"));
                //For UserLevel != 0, WorkshopID must be NULL
                if (_viewModel.UserLevel != 0 && _viewModel.WorkshopID != null)
                    errors.Add(new ValidationError("User with this Role cannot belong to a Workshop", "WorkshopID"));

                _insertValidationErrors.AddRange(errors);
            }
            return errors.Count == 0;
        }

        public ValidationError[] GetInsertValidationErrors()
        {
            return _insertValidationErrors.ToArray();
        }

        public bool IsUpdateValid()
        {
            throw new NotImplementedException();
        }

        public ValidationError[] GetUpdateValidationErrors()
        {
            throw new NotImplementedException();
        }
    }
}